package initization

import (
	"fmt"
	"github.com/spf13/viper"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"weekly-exam/config"
	"weekly-exam/internal/model/dao"
)

var err error

func init() {
	ViperInit()
	MysqlInit()
}
func ViperInit() {
	viper.SetConfigFile("D:\\goWork\\weekly-exam\\dev.yaml")
	viper.ReadInConfig()
	viper.Unmarshal(&config.AppConfig)
}
func MysqlInit() {
	app := config.AppConfig.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", app.User, app.Password, app.Host, app.Port, app.Database)
	config.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		fmt.Println("数据库连接失败")
		return
	} else {
		fmt.Println("数据库连接成功")
	}
	config.DB.AutoMigrate(&dao.User{}, &dao.Friend{}, &dao.Message{})
}
